package com.alex.xmlyfm.adapters;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alex.xmlyfm.R;
import com.alex.xmlyfm.entities.albumdetail.AlbumDetailTrack;

import java.util.List;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-23.
 */
public class AlbumDetailAdapter extends BaseAdapter {
    private List<AlbumDetailTrack> tracks;
    private Context context;
    public AlbumDetailAdapter(List<AlbumDetailTrack> tracks, Context context) {
        this.tracks = tracks;
        this.context = context;
    }



//    private View.OnClickListener onClickListener;
//
//    public void setOnMusicPlayListener(View.OnClickListener onClickListener) {
//        this.onClickListener = onClickListener;
//    }

    public void addList(List<AlbumDetailTrack> list){
        tracks.addAll(list);
    }

    @Override
    public int getCount() {
        return tracks.size();
    }

    @Override
    public Object getItem(int position) {
        return tracks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.fragment_album_detail_item,parent,false);
        }
        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.txtTitle = (TextView) ret.findViewById(R.id.txt_album_detail_title);
            holder.txtLast = (TextView) ret.findViewById(R.id.txt_album_detail_last);
            holder.txtPlayCommend = (TextView) ret.findViewById(R.id.txt_album_detail_play_commend);
            holder.txtPlayNum = (TextView) ret.findViewById(R.id.txt_album_detail_play_num);
            holder.txtPlayTime = (TextView) ret.findViewById(R.id.txt_album_detail_play_time);
            holder.imgDownload = (ImageView) ret.findViewById(R.id.img_album_detail_download);
            holder.imgPlay = (ImageView) ret.findViewById(R.id.img_album_detail_play);
            holder.imgPlay.setTag(position);
            ret.setTag(holder);
        }


        AnimationDrawable drawable;
        drawable = (AnimationDrawable) holder.imgPlay.getBackground();
        AlbumDetailTrack track = tracks.get(position);
        if (track.isPlay()){
            holder.imgPlay.setVisibility(View.VISIBLE);
            drawable.start();
        }else{
            holder.imgPlay.setVisibility(View.INVISIBLE);
        }

        holder.txtTitle.setText(track.getTitle());

        return ret;
    }
    private static class ViewHolder{
        TextView txtTitle,txtLast,txtPlayNum,txtPlayTime,txtPlayCommend;
        ImageView imgPlay,imgDownload;
    }
}
